//
//  TopMainView.m
//  RestaurantCar
//
//  Created by apple on 2017/5/9.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "TopMainView.h"

#define QUARTER self.frame.size.width / 4
#define HEIGHT self.frame.size.height


@implementation TopMainView
{
    NSArray *arrTitle;
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addButton];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)addButton {
    self.arrayButton = [NSMutableArray array];
    arrTitle = @[@"全部",@"早餐车",@"餐车",@"优惠"];
    for (int i = 0 ; i < 4 ; i++) {
        self.chooseButton = [[UIButton alloc] initWithFrame:CGRectMake(QUARTER * i, 0, QUARTER, HEIGHT)];
        self.chooseButton.tag = i;
        [self.chooseButton setTitle:arrTitle[i] forState:UIControlStateNormal];
        [self.chooseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.chooseButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.chooseButton setTitleColor:[UIColor colorWithRed:253/255.0 green:154/255.0 blue:26/255.0 alpha:1]    forState:UIControlStateSelected];
        if (0 == self.chooseButton.tag) {
           self.chooseButton.selected = YES;
        }
        
        [self.chooseButton addTarget:self action:@selector(makeButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.arrayButton addObject:self.chooseButton];
        [self addSubview:self.chooseButton];
    }
}
- (void)makeButton:(UIButton *)sender {
    for (UIButton *button in self.arrayButton) {
        
        if (button.tag == sender.tag) {
            button.selected = YES;
            
            if ([self.delegate respondsToSelector:@selector(selectedButtonTag:)]) {
                [self.delegate selectedButtonTag:sender.tag];
            }
            
        } else {
            button.selected = NO;
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
